package com.longrise.android.byjk.plugins.dealsituation.course.video2.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.AppBaseDialog;

/**
 * Created by godliness on 2017/7/4.
 */

public class UploadDialog extends AppBaseDialog implements View.OnClickListener {

    private static final String TAG = "UploadDialog";

    /**
     * 三种形式的界面类型
     */
    public static final int DIALOG_TYPE_ONE = 0;  //同步记录
    public static final int DIALOG_TYPE_SEC = 1;  //返回课程详情  同步学习记录
    public static final int DIALOG_TYPE_THR = 2;  //返回课程详情

    private TextView mTvCommit;
    private TextView mTvBack;

    private DialogUploadInterface mUpload;

    private int mCurrentType = DIALOG_TYPE_ONE;

    public UploadDialog(Context context, int type) {
        super(context, R.style.VideoListDialog);
        setCanceledOnTouchOutside(false);
        this.mCurrentType = type;
    }

    @Override
    public int getLayoutResourceId(Bundle bundle) {
        int layoutResource = R.layout.dialog_type_one;
        switch (mCurrentType) {
            case DIALOG_TYPE_ONE:
                layoutResource = R.layout.dialog_type_one;
                break;

            case DIALOG_TYPE_SEC:
                layoutResource = R.layout.dialog_type_sec;
                break;

            case DIALOG_TYPE_THR:
                layoutResource = R.layout.dialog_type_thr;
                break;

            default:
        }
        return layoutResource;
    }

    @Override
    public void init() {
        switch (mCurrentType) {
            case DIALOG_TYPE_ONE:
                mTvCommit = (TextView) findViewById(R.id.tv_commit_upload);
                break;

            case DIALOG_TYPE_SEC:
                mTvCommit = (TextView) findViewById(R.id.tv_commit_upload);
                mTvBack = (TextView) findViewById(R.id.tv_back_upload);
                break;

            case DIALOG_TYPE_THR:
                mTvBack = (TextView) findViewById(R.id.tv_back_upload);
                break;

            default:
        }
    }

    @Override
    public void regEvent() {
        if (mTvCommit != null) {
            mTvCommit.setOnClickListener(this);
        }
        if (mTvBack != null) {
            mTvBack.setOnClickListener(this);
        }
    }

    /**
     * 同步记录
     */
    public void onCommit() {
        if (!NetUtil.checkNetEnable()) {
            return;
        }
        dismiss();
        if (mUpload != null) {
            mUpload.onUpload();
        }
    }

    /**
     * 返回详情
     */
    public void onBack() {
        dismiss();
        if (mUpload != null) {
            mUpload.back();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            if (mUpload != null) {
                mUpload.back();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_commit_upload:  //同步学习记录
                onCommit();
                break;

            case R.id.tv_back_upload:  //返回课程详情
                onBack();
                break;

            default:
        }
    }

    public void setUploadInterface(DialogUploadInterface uploadInterface) {
        this.mUpload = uploadInterface;
    }

    public interface DialogUploadInterface {

        void onUpload();

        void back();
    }
}
